package cn.zust.itcost.controller;

import cn.zust.itcost.common.R;
import cn.zust.itcost.entity.Administrators;
import cn.zust.itcost.service.AdministratorsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/*管理员管理*/

@Slf4j
@RestController
@RequestMapping("/administrators")
public class AdministratorsController {

    @Autowired
    private AdministratorsService administratorsService;

    /**
     * 管理员登录
     *
     * @param request
     * @param administrators
     * @return
     */
    @PostMapping("/login")
    public R<Object> login(HttpServletRequest request, @RequestBody Administrators administrators){
        //对前端密码加密
        String password = administrators.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //根据用户名查数据库
        LambdaQueryWrapper<Administrators> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(Administrators::getName,administrators.getName());//等值查询
        Administrators administrators1 = administratorsService.getOne(queryWrapper);

        //数据是否存在
        if (administrators1 == null){
            return R.fail().message("登录失败,用户不存在");
        }

        //密码比对
        if (!administrators1.getPassword().equals(password)){
            return R.fail().message("密码错误");
        }

        //登录成功，id放入Session，返回结果
        request.getSession().setAttribute("employee",administrators1.getId());
        return R.ok(administrators1);
    }

    /**
     * 创建二级管理员
     *
     * @param request
     * @param administrators
     * @return
     */
    @PostMapping("/create")
    public R<Object> create(HttpServletRequest request, @RequestBody Administrators administrators){
        log.info("新增员工，员工信息：{}",administrators.toString());
        //设置初始密码123456，需要md5加密
        administrators.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        administrators.setCreattime(LocalDateTime.now());
        administrators.setState("启用");

        administratorsService.save(administrators);

        return R.ok().message("新增管理员成功");
    }

    /**
     * 查询二级管理员
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public R<List<Administrators>> page(int page, int pageSize){

        //构造分页构造器对象
        Page<Administrators> pageInfo = new Page<>(page, pageSize);

        //条件构造器
        LambdaQueryWrapper<Administrators> queryWrapper = new LambdaQueryWrapper<>();

        //添加查询条件：id不等于1
        queryWrapper.ne(Administrators::getId, 1);

        //添加排序条件
        queryWrapper.orderByDesc(Administrators::getCreattime);

        //执行分页查询
        administratorsService.page(pageInfo, queryWrapper);

        //获取查询结果列表
        List<Administrators> records = pageInfo.getRecords();

        return R.ok(records);
    }

    /**
     * 修改二级管理员
     *
     * @param administrators
     * @return
     */
    @PutMapping("/updata")
    public R<Object> updata(@RequestBody Administrators administrators){
        log.info(administrators.toString());

        //合法判断
        Long id = administrators.getId();
        if (id!=null){
            LambdaQueryWrapper<Administrators> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Administrators::getId,id);
            if (queryWrapper==null){
                return R.fail().message("无此管理员");
            }
        }else {
            return R.fail().message("处理异常");
        }

        administrators.setPassword(DigestUtils.md5DigestAsHex(administrators.getPassword().getBytes()));
        administratorsService.updateById(administrators);

        return R.ok(administrators);
    }

    /**
     * 删除二级管理员
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R<Object> deleteEmployee(@PathVariable Long id) {
        boolean result = administratorsService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.fail().message("删除失败");
        }
    }

}
